package com.brokendust.client.api.module.item;

import com.brokendust.client.PixelEngine;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fml.client.gui.GuiUtils;

import java.util.List;

public class ItemTool {
    public int blitOffset;

    protected void renderTooltip(ItemStack itemStack, int p_renderTooltip_2_, int p_renderTooltip_3_) {
        FontRenderer font = itemStack.getItem().getFontRenderer(itemStack);
        GuiUtils.preItemToolTip(itemStack);
        this.renderTooltip(this.getTooltipFromItem(itemStack), p_renderTooltip_2_, p_renderTooltip_3_, (font == null) ? PixelEngine.fontRenderer : font);
        GuiUtils.postItemToolTip();
    }

    public void renderTooltip(List<String> itemStringList, int fX, int fY, FontRenderer font) {
        RenderSystem.disableRescaleNormal();
        RenderSystem.disableDepthTest();
        int i = 0;
        for (String s : itemStringList) {
            int j = PixelEngine.fontRenderer.width(s);
            if (j > i) {
                i = j;
            }
        }
        int l1 = fX;
        int i2 = fY;
        int k = 8;
        if (itemStringList.size() > 1) {
            k += 2 + (itemStringList.size() - 1) * 10;
        }
        this.setBlitOffset(300);
        PixelEngine.itemRenderer.blitOffset = 300.0f;
        int m = -267386864;
        this.fillGradient(l1 - 3, i2 - 4, l1 + i + 3, i2 - 3, -267386864, -267386864);
        this.fillGradient(l1 - 3, i2 + k + 3, l1 + i + 3, i2 + k + 4, -267386864, -267386864);
        this.fillGradient(l1 - 3, i2 - 3, l1 + i + 3, i2 + k + 3, -267386864, -267386864);
        this.fillGradient(l1 - 4, i2 - 3, l1 - 3, i2 + k + 3, -267386864, -267386864);
        this.fillGradient(l1 + i + 3, i2 - 3, l1 + i + 4, i2 + k + 3, -267386864, -267386864);
        int i3 = 1347420415;
        int j2 = 1344798847;
        this.fillGradient(l1 - 3, i2 - 3 + 1, l1 - 3 + 1, i2 + k + 3 - 1, 1347420415, 1344798847);
        this.fillGradient(l1 + i + 2, i2 - 3 + 1, l1 + i + 3, i2 + k + 3 - 1, 1347420415, 1344798847);
        this.fillGradient(l1 - 3, i2 - 3, l1 + i + 3, i2 - 3 + 1, 1347420415, 1347420415);
        this.fillGradient(l1 - 3, i2 + k + 2, l1 + i + 3, i2 + k + 3, 1344798847, 1344798847);
        MatrixStack matrixstack = new MatrixStack();
        IRenderTypeBuffer.Impl irendertypebuffer$impl = IRenderTypeBuffer.immediate(Tessellator.getInstance().getBuilder());
        matrixstack.translate(0.0, 0.0, PixelEngine.itemRenderer.blitOffset);
        Matrix4f matrix4f = matrixstack.last().pose();
        for (int k2 = 0; k2 < itemStringList.size(); ++k2) {
            String s2 = itemStringList.get(k2);
            if (s2 != null) {
                PixelEngine.fontRenderer.drawInBatch(s2, (float) l1, (float) i2, -1, true, matrix4f, irendertypebuffer$impl, false, 0, 15728880);
            }
            if (k2 == 0) {
                i2 += 2;
            }
            i2 += 10;
        }
        irendertypebuffer$impl.endBatch();
        this.setBlitOffset(0);
        PixelEngine.itemRenderer.blitOffset = 0.0f;
        RenderSystem.enableDepthTest();
        RenderSystem.enableRescaleNormal();
    }

    public void setBlitOffset(int blitOffset) {
        this.blitOffset = blitOffset;
    }

    public List<String> getTooltipFromItem(ItemStack itemStack) {
        List<ITextComponent> list = itemStack.getTooltipLines(PixelEngine.minecraft.player, PixelEngine.minecraft.options.advancedItemTooltips ? ITooltipFlag.TooltipFlags.ADVANCED : ITooltipFlag.TooltipFlags.NORMAL);
        List<String> list2 = Lists.newArrayList();
        for (ITextComponent itextcomponent : list) {
            list2.add(itextcomponent.getString());
        }
        return list2;
    }

    protected void fillGradient(int p_fillGradient_1_, int p_fillGradient_2_, int p_fillGradient_3_, int p_fillGradient_4_, int p_fillGradient_5_, int p_fillGradient_6_) {
        float f = (p_fillGradient_5_ >> 24 & 0xFF) / 255.0f;
        float f2 = (p_fillGradient_5_ >> 16 & 0xFF) / 255.0f;
        float f3 = (p_fillGradient_5_ >> 8 & 0xFF) / 255.0f;
        float f4 = (p_fillGradient_5_ & 0xFF) / 255.0f;
        float f5 = (p_fillGradient_6_ >> 24 & 0xFF) / 255.0f;
        float f6 = (p_fillGradient_6_ >> 16 & 0xFF) / 255.0f;
        float f7 = (p_fillGradient_6_ >> 8 & 0xFF) / 255.0f;
        float f8 = (p_fillGradient_6_ & 0xFF) / 255.0f;
        RenderSystem.disableTexture();
        RenderSystem.enableBlend();
        RenderSystem.disableAlphaTest();
        RenderSystem.defaultBlendFunc();
        RenderSystem.shadeModel(7425);
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferbuilder = tessellator.getBuilder();
        bufferbuilder.begin(7, DefaultVertexFormats.POSITION_COLOR);
        bufferbuilder
                .normal(p_fillGradient_3_, p_fillGradient_2_, this.blitOffset)
                .color(f2, f3, f4, f)
                .endVertex();
        bufferbuilder
                .normal(p_fillGradient_1_, p_fillGradient_2_, this.blitOffset)
                .color(f2, f3, f4, f)
                .endVertex();
        bufferbuilder
                .normal(p_fillGradient_1_, p_fillGradient_4_, this.blitOffset)
                .color(f6, f7, f8, f5)
                .endVertex();
        bufferbuilder
                .normal(p_fillGradient_3_, p_fillGradient_4_, this.blitOffset)
                .color(f6, f7, f8, f5)
                .endVertex();
        tessellator.end();
        RenderSystem.shadeModel(7424);
        RenderSystem.disableBlend();
        RenderSystem.enableAlphaTest();
        RenderSystem.enableTexture();
    }
}
